<?php
/**
 * Created by PhpStorm.
 * User: zlkmu
 * Date: 2018/1/31
 * Time: 9:36
 */
require_once __DIR__.'/../Module/User.php';

class ServerContext{
    //存放当前服务器在线玩家的数组
    private $currentServerPlayers = array();

    /**
     * 增加在线玩家的函数
     * @param $playerAvatar PlayerAvatar
     * @return mixed uid/false
     */
    public function addPlayerAvatar($clientPlayerInfo, $ip){
        $newPlayerAvatar = new PlayerAvatar($clientPlayerInfo, $ip);
        $playerID = $newPlayerAvatar->getPlayerUID();
        if(array_key_exists($playerID, $this->currentServerPlayers)){
            return false;
        }
        else{
            $this->currentServerPlayers[$playerID] = $newPlayerAvatar;
            return $newPlayerAvatar->getPlayerUID();
        }
    }


    /**
     * 玩家上线的函数（从掉线转为在线)
     * @param $playerID
     * @return bool true/false
     */
    public function playerOnLine($playerID){
        if(!array_key_exists($playerID, $this->currentServerPlayerss)){
            return false;
        }
        $this->currentServerPlayers[$playerID]->OnLine();
        return true;
    }

    /**
     * 玩家下线的函数(从在线转为掉线)
     * @param $playerID
     * @return bool true/false
     */
    public function playerOffLine($playerID){
        if(!array_key_exists($playerID, $this->currentServerPlayers)){
            return false;
        }
        $this->currentServerPlayers[$playerID]->OffLine();
        return true;
    }

    /**
     * 玩家退出登录的函数
     * @param $playerID
     * @return bool true/false
     */
    public function playerLogOut($playerID){
        if(!array_key_exists($playerID, $this->currentServerPlayers)){
            return false;
        }
        $this->currentServerPlayers[$playerID]->playerLogOut();
        return true;
    }

    /**
     * 获取某个玩家的信息
     * @param $playerID string 玩家信息
     * @return mixed 玩家的playerAvatar或者null
     */
    public function getPlayer($playerID){
        if(array_key_exists($playerID, $this->currentServerPlayers))
            return $this->currentServerPlayers[$playerID];
        else{
            return null;
        }
    }

    /**
     * 获取当前服务器在线玩家的人数
     * @return int 玩家人数
     */
    public function getPlayerCount(){
        return count($this->currentServerPlayers);
    }

    /**
     * 判断某个玩家是否在线
     * @param $playerID 玩家ID
     * @return mixed true/false
     */
    public function isPlayerOnLine($playerID){
        return $this->currentServerPlayers[$playerID]->isPlayerOnLine();
    }

    /**
     * 更新玩家的分享次数
     * @param $playerID
     * @return int 0=>状态(1,当天分享过，但可以继续分享; 2,时间间隔不够，不能分享; 3,分享次数满，不能分享)
     *
     */
    public function updatePlayerShareTime($playerID){
        return $this->currentServerPlayers[$playerID]->updatePlayerShare();
    }

    /**
     * 玩家进行实名认证的函数
     * @param $playerID 玩家UID
     * @param $userName 玩家真实姓名
     * @param $idCard 玩家身份证号
     * @return bool true/false
     */
    public function realNameAuth($playerID, $userName, $idCard){
        return $this->currentServerPlayers[$playerID]->realNameAuth($playerID, $userName, $idCard);
    }

    /**
     * 钻石换房卡的函数
     * @param $playerID 玩家UID
     * @param $request 客户端请求
     * @return mixed array/false
     */
    public function diamondToCard($playerID, $request){
        return $this->currentServerPlayers[$playerID]->diamondToCard($request);
    }

    /**
     * 刷新玩家钻石和房卡
     * @param $playerID 玩家UID
     * @return mixed array/false
     */
    public function refreshDiamondAndCard($playerID)
    {
        return $this->currentServerPlayers[$playerID]->refreshDiamondAndCard();
    }


}





?>